<template>
	<view v-if="is_init">
		<uni-popup-input name="新增/编辑" @close="on_close">




			<view class="banmi_input_edit a-c">
				<view class="banmi_input_edit_name">
					<text class="cred">*</text>账号
				</view>
				<view class="banmi_input_val">
					<uni-easyinput v-model="find.account" placeholder="请输入账号">
					</uni-easyinput>
				</view>
			</view>
			<view class="banmi_input_edit a-c">
				<view class="banmi_input_edit_name">
					<text class="cred">*</text>密码
				</view>
				<view class="banmi_input_val">

					<uni-easyinput type="safe-password" v-model="find.password" placeholder="请输入密码">
					</uni-easyinput>
				</view>
			</view>
			<view class="banmi_input_edit a-c">
				<view class="banmi_input_edit_name">
					手机号
				</view>
				<view class="banmi_input_val">
					<uni-easyinput v-model="find.tel" placeholder="请输入手机号">
					</uni-easyinput>
				</view>
			</view>
			<view class="banmi_input_edit a-c">
				<view class="banmi_input_edit_name">
					昵称
				</view>
				<view class="banmi_input_val">
					<uni-easyinput v-model="find.nickname" placeholder="请输入昵称">
					</uni-easyinput>
				</view>
			</view>




			<view class="banmi_input_edit a-c">
				<view class="banmi_input_edit_name">
					头像
				</view>
				<view class="banmi_input_val">
					<upImg ref="upimg" :val='1' name="图片" :count="1" :list="find.face" t_type="images"
						@confirm="confirm_face">
					</upImg>
				</view>
			</view>
			<view class="banmi_input_edit a-c">
				<view class="banmi_input_edit_name">
					性别
				</view>
				<view class="banmi_input_val">
					<uni-data-checkbox :localdata="localdata_gender" @change="change_gender" :value="find.gender"
						mode="default"></uni-data-checkbox>
				</view>
			</view>

			<view class="banmi_input_edit a-c" v-if="uid>0">
				<view class="banmi_input_edit_name">
					余额
				</view>
				<view class="  banmi_flex a-c">

					<text>{{user_money.money/100}}元</text>
					<button @click="on_set_money(1,'请输入增加的余额')" class="f-14 banmi_flex a-c j-c b_y cff add_money">
						增加余额+
					</button>
					<button @click="on_set_money(0,'请输入减少的余额')" class="f-14 banmi_flex a-c j-c b_y cff add_money"
						style="background-color: #228B22;">
						减少余额-
					</button>
				</view>
			</view>




			<view class="a-c  p-b-20 banmi_input_edit">
				<view class="banmi_input_edit_name">
				</view>
				<view class="">
					<button @click="on_sava()" type="primary" style="width: 80px;" size="mini">保存</button>
				</view>
			</view>

		</uni-popup-input>
		<!-- 	颜色选择器 -->
		<t-color-picker ref="colorPicker" @confirm="confirm_color"></t-color-picker>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-beizhu cancelText="取消" confirmText="确认" lei="0" :name="name" :keya="keya" placeholder="请输入金额"
				@confirm="on_money">
			</uni-popup-beizhu>
		</uni-popup>

	</view>
</template>

<script>
	import provinceData from '@/components/fd-picker/city-china.json';
	var find = {
		tel: '',
		account: '',
		nickname: '',
		face: '',
		face_type: 0,
		gender: 0,
		master_oen: 0,
		master_two: 0

	};
	export default {
		components: {},
		props: {

		},
		data() {
			return {
				find: {},
				uid: 0,

				is_init: false,
				nav_list: [],
				img_url: '',
				localdata_gender: [{
						"value": 0,
						"text": "未知",
					},
					{
						"value": 1,
						"text": "男",
					},
					{
						"value": 2,
						"text": "女",
					},
				],
				screenWidth: 0,
				name: '',
				state: '',
				keya: 'add_money',
				user_money: {}

			}
		},

		methods: {
			on_money(val, text) {
				if (this.keya == 'add_money') {
					if (!text) {
						uni.showToast({
							title: '请输入操作原因',
							icon: 'error'
						})
						return
					}
					this.number_add(val, text);
					return
				}



			},
			async number_add(val, text) {
				let requestParams = {
					uid: this.uid,
					state: this.state,
					number: val,
					text: text
				};
				const ret = await this.Banmikeji.user_edit_money(requestParams);
				if (ret.status == 200) {
					val = val * 100
					if (this.state == 1) {
						this.user_money.money = Number(this.user_money.money) + Number(val);
					} else {
						this.user_money.money = Number(this.user_money.money) - Number(val);
					}
					this.$refs.alertDialog.close()
				}
			},
			on_set_money(state, name) {
				this.state = state;
				this.name = name;
				this.keya = 'add_money';
				this.$refs.alertDialog.open()
			},
			change_gender(e) {
				this.find.gender = e.detail.value;
			},
			confirm_face(vals) {
				if (vals.length == 0) {
					this.find.face = ''
					return
				}
				this.find.face = vals[0]['attachment']
			},

			async on_sava() {
				var that = this;
				if (this.uid == 0 && !this.find.account) {
					uni.showToast({
						title: '请输入账号',
						icon: 'error'
					})
					return
				}
				if (this.uid == 0 && !this.find.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'error'
					})
					return
				}
				this.find.face = this.find.face.replace(uni.getStorageSync('img_url'), '');

				this.find.uid = this.uid

				const ret = await this.Banmikeji.user_user_add(this.uid, this.find);
				uni.showToast({
					title: ret.msg,
					icon: 'none'
				})
				if (ret.status == 200) {
					this.$emit("confirm");
					this.on_close();
				}


			},
			async init(uid) {
				console.log("uid: " + JSON.stringify(uid));
				var that = this;
				that.uid = uid
				if (that.uid == 0) {
					this.find = {};
					this.is_init = true;
					return
				}
				let requestParams = {
					uid: this.uid,
				};
				const ret = await this.Banmikeji.user_user_info(requestParams);
			 
				this.find = ret.data.users;

				this.user_money = ret.data.user_money;
				this.is_init = true;
			},

			on_close() {
				this.find = {}
				this.is_init = false;
			},
			//js分类方法

		}
	}
</script>

<style>
	button::after {
		border: none;
	}

	.top_head {
		background-color: #F8F8F8;
		width: 100%;
		top: 0;
		left: 0;
		z-index: 999;
		position: sticky
	}

	.but {
		width: 120rpx;
		height: 100rpx;
		padding: 0;
		border: 0;
		background-color: transparent;
		outline: none;
	}

	.add_money {
		background-color: #FF0000;
		width: 120px;
		height: 30px;
		border-radius: 25px;
		margin-left: 10px;
	}
</style>